package controllers;

import configuration.FrontContainer;
import factories.IPlotFactory;
import handlers.IFileInfoHandler;
import handlers.StatisticsHandler;
import models.FileInfo;
import models.PlotModel;
import models.statistics.*;
import play.mvc.Controller;

import java.util.List;

import static helpers.JsonHelpers.toJSON;

public class Statistic extends Controller {

    public static void graphics() {
        StatisticsHandler statisticsHandler = new StatisticsHandler(FrontContainer.getInstance());
        IPlotFactory plotFactory = FrontContainer.getInstance().get(IPlotFactory.class);

        LZ77Statistics[] lz77Results = statisticsHandler.getStatistics(LZ77Statistics.class).toArray(new LZ77Statistics[0]);
        LZMAStatistics[] lzmaResults = statisticsHandler.getStatistics(LZMAStatistics.class).toArray(new LZMAStatistics[0]);
        LZWStatistics[] lzwResults = statisticsHandler.getStatistics(LZWStatistics.class).toArray(new LZWStatistics[0]);
        SLPClassicStatistics[] slpClassicResults = statisticsHandler.getStatistics(SLPClassicStatistics.class).toArray(new SLPClassicStatistics[0]);
        SLPOptimalStatistics[] slpOptimalResults = statisticsHandler.getStatistics(LZ77Statistics.class).toArray(new SLPOptimalStatistics[0]);
        SLPCartesianStatistics[] slpCartesianResults = statisticsHandler.getStatistics(LZ77Statistics.class).toArray(new SLPCartesianStatistics[0]);

        PlotModel avlRotationsPlot = plotFactory.createAVLRotationsPlot(slpClassicResults, slpOptimalResults);
        PlotModel slpCompressionTimePlot = plotFactory.createSLPCompressionTimePlot(slpClassicResults, slpOptimalResults);
        PlotModel compressionRatePlot = plotFactory.createCompressionRatePlot(lz77Results, lzmaResults, lzwResults, slpClassicResults, slpOptimalResults, slpCartesianResults);

        renderArgs.put("AvlRotationsPlot", toJSON(avlRotationsPlot));
        renderArgs.put("SLPCompressionTimePlot", toJSON(slpCompressionTimePlot));
        renderArgs.put("CompressionRatePlot", toJSON(compressionRatePlot));

        render("Statistic/graphics.html");
    }

    public static void filesInfo() {
        IFileInfoHandler fileInfoHandler = FrontContainer.getInstance().get(IFileInfoHandler.class);
        List<FileInfo> filesInfo = fileInfoHandler.find();
        render("Statistic/filesInfo.html", filesInfo);
    }
}
